# encoding=utf-8

# ---------------------------------------------------------------------------------------------------------
#
# 程序入口
#
# ---------------------------------------------------------------------------------------------------------

import logging

from core import document, markdown, deploy, reference
import config


def _init():
    '''初始化日志配置
    '''
    level = logging.DEBUG if config.DEBUG else logging.INFO
    logging.basicConfig(level=level,
                        format='%(asctime)s %(filename)s [%(lineno)d] %(levelname)s %(message)s',
                        datefmt='%Y/%m/%d %H:%M:%S',
                        filemode='w')

    if config.DEBUG:
        logging.warning('检测到已开启调试模式，将打印相关调试信息')

    if config.FORCE_UPDATE_HANDBOOK:
        logging.warning('检测到已开启强制升级配置项"FORCE_UPDATE_HANDBOOK"，程序将对所有手册文档进行强制发布')

    if config.FORCE_UPDATE_REFERENCE:
        logging.warning('检测到已开启强制升级配置项"FORCE_UPDATE_REFERENCE"，程序将对所有引用文档进行强制发布')


if __name__ == '__main__':
    '''程序入口
    '''

    # 启动前准备
    _init()

    # 转换引用文档
    dh_1 = document.Handler(config.REFERENCE_PATH, True)
    changed_1, deleted_1, documents_1 = dh_1.handle()

    # 转换手册文档
    dh_2 = document.Handler(config.HANDBOOK_PATH, False)
    changed_2, deleted_2, documents_2 = dh_2.handle()

    # 更新引用
    rh = reference.Handler()
    rh.reference_documents = documents_1
    rh.handbook_documents = documents_2
    rh.changed_handbook_documents = changed_2
    rh.handle()

    # 文档拆分
    mh_1 = markdown.Handler(config.REFERENCE_PATH, True, changed_1, documents_1)
    markdowns_1 = mh_1.handle()

    mh_2 = markdown.Handler(config.HANDBOOK_PATH, False, changed_2, documents_2)
    markdowns_2 = mh_2.handle()

    # 发布文档
    d_1 = deploy.Handler(config.REFERENCE_PATH, True, markdowns_1, deleted_1)
    d_1.handle()

    d_2 = deploy.Handler(config.HANDBOOK_PATH, False, markdowns_2, deleted_2)
    d_2.handle()
